package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.query.ClientQueryObject;
import cn.wolfcode.crm.service.IClientService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @program: crm
 * @description:
 * @author: Mr.chen
 * @create: 2018-07-07 22:06
 **/
@Controller
@RequestMapping("client")
public class ClientController {
    @Autowired
    private IClientService service;

    @RequestMapping("")
    @PermissionName("客户主页")
    @RequiresPermissions("client:index")
    public String index() {
        return "client/list";
    }

    @RequestMapping("list")
    @ResponseBody
    @RequiresPermissions("client:list")
    @PermissionName("客户数据")
    public PageResult list(ClientQueryObject qo) {
        qo.setState1(5);
        PageResult result = service.query(qo);
        return result;
    }

    @RequestMapping("update")
    @ResponseBody
    @RequiresPermissions("client:update")
    @PermissionName("客户信息更新")
    public JsonResult update(Client entity) {
        JsonResult result = new JsonResult();
        try {
            service.update(entity);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("更新操作失败");
        }
        return result;
    }

    @RequestMapping("changeState")
    @ResponseBody
    @RequiresPermissions("client:changeState")
    @PermissionName("客户状态修改")
    public JsonResult changeState(Long id,int state) {
        JsonResult result = new JsonResult();
        try {
            service.changeState(id,state);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("状态变更失败");
        }
        return result;
    }

    @RequestMapping("queryClients")
    @ResponseBody
    public List<Client> queryClients(){
        List<Client> list = service.list();
        return list;
    }

}
